package com.pope.framework.triage.modular.service;

import com.pope.framework.core.util.StringUtil;
import com.pope.framework.dict.modular.entity.DictAllergic;
import com.pope.framework.dict.modular.entity.DictMedical;
import com.pope.framework.dict.modular.service.DictAllergicService;
import com.pope.framework.dict.modular.service.DictMedicalService;
import com.pope.framework.kernel.model.exception.ServiceException;
import com.pope.framework.triage.modular.dto.PatientInfoDto;
import com.pope.framework.triage.modular.entity.PatientAllergic;
import com.pope.framework.triage.modular.entity.PatientInfo;
import com.pope.framework.triage.modular.entity.PatientMedical;
import com.pope.framework.triage.modular.entity.TriageInfo;
import com.pope.framework.triage.modular.mapper.PatientInfoMapper;
import com.pope.framework.triage.modular.vo.DictVo;
import com.pope.framework.triage.modular.vo.PatientInfoVo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 患者信息表 服务实现类
 * </p>
 *
 * @author superpope
 * @since 2019-01-14
 */
@Service
@Transactional
public class PatientInfoService extends ServiceImpl<PatientInfoMapper, PatientInfo> {

	@Autowired
	private PatientMedicalService patientMedicalService;
	
	@Autowired
	private PatientAllergicService patientAllergicService;
	
	@Autowired
	private DictMedicalService dictMedicalService;
	@Autowired
	private DictAllergicService dictAllergicService;
	@Autowired
	private TriageInfoService triageInfoService;
	
	
	
	private void deletePaientInfoExtend(PatientInfo paitentInfo) {
		PatientAllergic deletePatientAllergic=new PatientAllergic();
		deletePatientAllergic.setPatientId(paitentInfo.getId());
		UpdateWrapper<PatientAllergic> deletePatientAllergicWrapper=new UpdateWrapper<>();
		deletePatientAllergicWrapper.setEntity(deletePatientAllergic);
		this.patientAllergicService.remove(deletePatientAllergicWrapper);
		
		PatientMedical deletePatientMedical=new PatientMedical();
		deletePatientMedical.setPatientId(paitentInfo.getId());
		
		UpdateWrapper<PatientMedical> deletePatientMedicalWrapper=new UpdateWrapper<>();
		deletePatientMedicalWrapper.setEntity(deletePatientMedical);
		this.patientMedicalService.remove(deletePatientMedicalWrapper);
		
	}
	
	private void savePaientInfoExtend(PatientInfoDto patientInfoDto,PatientInfo savePatientInfo) {
		List<PatientAllergic> patientAllergics=CollUtil.newArrayList();
		if(CollUtil.isNotEmpty(patientInfoDto.getPatientAllergic())) {
			patientInfoDto.getPatientAllergic().forEach(s->{
				PatientAllergic patientAllergic=new PatientAllergic();
				patientAllergics.add(patientAllergic);
				patientAllergic.setAllergicId(s);
				patientAllergic.setPatientId(savePatientInfo.getId());
			});
			patientAllergicService.saveBatch(patientAllergics);
		}
		
		
		List<PatientMedical> patientMedicals=CollUtil.newArrayList();
		if(CollUtil.isNotEmpty(patientInfoDto.getPatientMedicals())) {
			patientInfoDto.getPatientMedicals().forEach(s->{
				PatientMedical patientMedical=new PatientMedical();
				patientMedicals.add(patientMedical);
				patientMedical.setMedicalId(s);
				patientMedical.setPatientId(savePatientInfo.getId());
			});
			patientMedicalService.saveBatch(patientMedicals);
		}
	}
	public Integer insert(PatientInfoDto patientInfoDto) {
		PatientInfo queryPatientInfo=new PatientInfo();
		queryPatientInfo.setIdNumber(patientInfoDto.getIdNumber());
		queryPatientInfo.setHospitalId(patientInfoDto.getHospitalId());
		QueryWrapper<PatientInfo> queryPatientInfoWrapper=new QueryWrapper<>();
		queryPatientInfoWrapper.setEntity(queryPatientInfo);
		List<PatientInfo> lst=this.list(queryPatientInfoWrapper);
		if(CollUtil.isNotEmpty(lst)) {
			throw new ServiceException("该身份证号已存在，请重新确认！");
		}
		
		PatientInfo savePatientInfo=new PatientInfo();
		BeanUtil.copyProperties(patientInfoDto, savePatientInfo);
		boolean flag=this.save(savePatientInfo);
		if(!flag) {
			throw new ServiceException("患者信息添加失败，请重新确认！");
		}
		this.savePaientInfoExtend(patientInfoDto, savePatientInfo);
		
		return savePatientInfo.getId();
	}
	
	public Integer modify(PatientInfoDto patientInfoDto) {
		
		if(StrUtil.isNotEmpty(patientInfoDto.getIdNumber())) {
			PatientInfo queryPatientInfo=new PatientInfo();
			queryPatientInfo.setIdNumber(patientInfoDto.getIdNumber());
			queryPatientInfo.setHospitalId(patientInfoDto.getHospitalId());
			QueryWrapper<PatientInfo> queryPatientInfoWrapper=new QueryWrapper<>();
			queryPatientInfoWrapper.setEntity(queryPatientInfo);
			List<PatientInfo> lst=this.list(queryPatientInfoWrapper);
			if(CollUtil.isNotEmpty(lst)) {
				lst.forEach(aPatientInfo->{
					if(aPatientInfo.getIdNumber().equals(patientInfoDto.getIdNumber())) {
						if(!aPatientInfo.getId().equals(patientInfoDto.getId())) {
							throw new ServiceException("该身份证号已存在，请重新确认！");
						}
					}
				});
			}
		}
		PatientInfo savePatientInfo=new PatientInfo();
		BeanUtil.copyProperties(patientInfoDto, savePatientInfo);
		savePatientInfo.setCreatetime(null);
		savePatientInfo.setCreateMan(null);
		
		UpdateWrapper<PatientInfo> uw = new UpdateWrapper<>();
		uw.set("BIRTHDAY", patientInfoDto.getBirthday());
		uw.set("AGE", patientInfoDto.getAge());
		uw.set("MEDICAL_CARD_NUMBER", patientInfoDto.getMedicalCardNumber());
		uw.set("LINK_MAN", patientInfoDto.getLinkMan());
		uw.set("TELEPHONE", patientInfoDto.getTelephone());
		uw.set("WORK_UNIT", patientInfoDto.getWorkUnit());
		uw.set("NATION", patientInfoDto.getNation());
		uw.set("HOME_ADDRESS", patientInfoDto.getHomeAddress());
		uw.set("NAME", patientInfoDto.getName());
		uw.set("ID_NUMBER", patientInfoDto.getIdNumber());
		uw.set("SEX", patientInfoDto.getSex());
		uw.eq("ID", patientInfoDto.getId());
		this.baseMapper.update(savePatientInfo, uw);
//		//boolean flag=this.updateById(savePatientInfo);
//		if(!flag) {
//			throw new ServiceException("患者信息修改失败，请重新确认!");
//		}
		this.deletePaientInfoExtend(savePatientInfo);
		this.savePaientInfoExtend(patientInfoDto, savePatientInfo);
		return savePatientInfo.getId();
	}
	public boolean delete(String id) {
		if(StrUtil.isEmpty(id)) {
			throw new ServiceException("ID主键不能为空，请重新确认！");
		}
		PatientInfo patientInfo=this.getById(id);
		if(patientInfo==null) {
			throw new ServiceException("该患者信息不存在，请重写确认！");
		}
		TriageInfo triageInfo=new TriageInfo();
		triageInfo.setPatientId(StringUtil.toInt(id));
		QueryWrapper<TriageInfo> queryWrapper=new QueryWrapper<>();
		queryWrapper.setEntity(triageInfo);
		int count=triageInfoService.count(queryWrapper);
		if(count>0) {
			throw new ServiceException("该患者信息已存在分诊记录，您无法删除该患者信息！");
		}
		this.deletePaientInfoExtend(patientInfo);
		boolean flag=this.removeById(id);
		if(!flag) {
			throw new ServiceException("该患者信息不存在，请重写确认！");
		}
		return flag;
	}
	
	public PatientInfoVo getPatientDisplay(PatientInfo patientInfo) {
		if(patientInfo==null) {
			throw new ServiceException("该患者信息已不存在，请重新确认！");
		}
		List<DictMedical> dictMedicals=dictMedicalService.list();
		List<DictAllergic> dictAllergics=dictAllergicService.list();
		PatientInfoVo patientInfoVo=new PatientInfoVo();
		BeanUtil.copyProperties(patientInfo, patientInfoVo);
		//获取病史信息
		PatientMedical queryPatientMedical=new PatientMedical();
		queryPatientMedical.setPatientId(patientInfo.getId());
		QueryWrapper<PatientMedical> patientMedicalWrapper=new QueryWrapper<>();
		patientMedicalWrapper.setEntity(queryPatientMedical);
		List<PatientMedical> patientMedicals= patientMedicalService.list(patientMedicalWrapper);
		if(CollUtil.isNotEmpty(patientMedicals) && CollUtil.isNotEmpty(dictMedicals)) {
			patientInfoVo.setDictMedicals(CollUtil.newArrayList());
			patientMedicals.forEach(patientMedical->{
					dictMedicals.forEach(dictMedical->{
						if(patientMedical.getMedicalId().equals(dictMedical.getCode())) {
							DictVo dictVo=new DictVo();
							BeanUtil.copyProperties(dictMedical, dictVo);
							patientInfoVo.getDictMedicals().add(dictVo);
						}
					});
			});
		}
		//获取过敏史
		
		PatientAllergic queryPatientAllergic=new PatientAllergic();
		queryPatientAllergic.setPatientId(patientInfo.getId());
		QueryWrapper<PatientAllergic> patientAllergicWrapper=new QueryWrapper<>();
		patientAllergicWrapper.setEntity(queryPatientAllergic);
		List<PatientAllergic> patientAllergics= patientAllergicService.list(patientAllergicWrapper);
		if(CollUtil.isNotEmpty(patientAllergics) && CollUtil.isNotEmpty(dictAllergics)) {
			patientInfoVo.setDictAllergics(CollUtil.newArrayList());
			patientAllergics.forEach(patientAllergic->{
					dictAllergics.forEach(dictAllergic->{
						if(patientAllergic.getAllergicId().equals(dictAllergic.getCode())) {
							DictVo dictVo=new DictVo();
							BeanUtil.copyProperties(dictAllergic, dictVo);
							patientInfoVo.getDictAllergics().add(dictVo);
						}
					});
			});
		}
		
		
		return patientInfoVo;
	}
}
